# -*- coding: utf-8 -*-
from odoo import api, fields, models
from odoo.exceptions import ValidationError


class AccountInvoiceInherit(models.Model):
    _inherit = 'account.invoice'

    taiwan_invoice = fields.Char(string='發票號碼')
    invoice_type = fields.Selection(selection=[(1, u'三聯式發票、電子計算機發票'), (2, u'收銀機發票(三聯式)及電子發票'),
                                               (3, u'二聯式發票、收銀機發票(二聯式)'), (4,u'減:退回及折讓')],
                                    string='發票類型', default=3)
    journal_company = fields.Many2one(related='journal_id.company_id')
    date_invoice = fields.Date(states={}, readonly=False, track_visibility='onchange')

    def test_set_invoice_type(self):
        for line in self.env['account.invoice'].search([]):
            line.invoice_type = 3


class AccountReportInherit(models.TransientModel):
    _inherit = 'account.report.general.ledger'

    gym_account_move = fields.Many2one(comodel_name='account.account', string='會計科目')


    # 學到新招，改變傳入的context，使其像在acount.acount勾選要顯示的會計科目後，只印以勾選的會計科目。
    def _print_report(self, data):
        new_context = self.env.context.copy()
        if self.gym_account_move.id:
            new_context['active_id'] = self.gym_account_move.id
            new_context['active_ids'] = self.gym_account_move.ids
            new_context['active_model'] = 'account.account'
        res = super(AccountReportInherit, self.with_context(new_context))._print_report(data)
        return res


class AccountVoucherInherit(models.Model):
    _inherit = 'account.voucher'

    taiwan_invoice = fields.Char(string='發票號碼')
    invoice_type = fields.Selection(selection=[(1, u'三聯式發票、電子計算機發票'), (2, u'收銀機發票(三聯式)及電子發票'),
                                               (3, u'二聯式發票、收銀機發票(二聯式)'), (4, u'減:退回及折讓')],
                                    string='發票類型', default=3)







